function $(s) {return document.getElementById(s);}
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/,"");}

function getAjax() {
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function ajaxOpen(url,func,async,get_post,post) {
//url,func, optional:async=true, get_post="GET", post=undefined
	var ajax=getAjax();
	ajax.onreadystatechange=function(){
		if(this.readyState==4 && this.status==200)
			func(this.responseText);
	}
	if(async==undefined)async=true;
	if(get_post==undefined)get_post="GET";
	ajax.open(get_post,url,async);
	if(get_post.toUpperCase()=="POST")
		ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	ajax.send(post);
}

function simpleAjaxDisplay(tag,url) {
	ajaxOpen(url,function(r){tag.innerHTML=r;});
}

function toggleBox(box_id,func) {
	var box=$(box_id);
	if(box.style.display=="none") {
		box.style.display="";
	} else {
		box.style.display="none";
	}
	if(box.innerHTML=='') func(box);
}

function toggleBoxClass(box,cls) {
	if(box.className.indexOf(cls)>=0)box.className=(" " + box.className + " ").replace(" " + cls + " "," ").trim();
	else box.className+=" " + cls;
}

//***** rate *****
function sendRate(box,node_id) {
	var state=$("shownode_rate_state");
	state.style.display="";
	state.innerHTML="updating...";
	simpleAjaxDisplay(state,"ajax.php?action=rate&node_id=" + node_id + "&value=" + box.value);
}

function toggleRateDetail(node_id) {
	toggleBox("shownode_rate_detail",function(detail_box) {
		detail_box.innerHTML="loading...";
		simpleAjaxDisplay(detail_box,"ajax.php?action=rate_detail&node_id=" + node_id);
	});
}

//********** tree list

function isSubNode(tr,nx) {
	return (nx != undefined && nx.className.indexOf(tr.id)>=0 && nx.children[0].style.paddingLeft>tr.children[0].style.paddingLeft);
}

function hideTableTree(now_tr) {
	var n=now_tr.nextSibling;
	while(n != undefined && n.nodeType == 3) n=n.nextSibling;
	while(isSubNode(now_tr,n)) {
		n=hideTableTree(n);
	}
	now_tr.style.display="none";
	return n;
}

function showTableTree(now_tr) {
	var n=now_tr.nextSibling;
	while(n != undefined && n.nodeType == 3) n=n.nextSibling;
	while(isSubNode(now_tr,n)) {
		if(now_tr.className.indexOf("tree-open")>=0)
			n=showTableTree(n);
		else
			n=hideTableTree(n);
	}
	now_tr.style.display="";
	return n;
}

//********group list-html convert
var KGL={
	USE_ID_1:1,
	ID_1_IS_GROUP:2,
	USE_ID_2:4,
	ID_2_IS_GROUP:8,
	OUTPUT_IS_CHILD:0x10,
	USE_DIRECT_LINK:0x40,
	USE_DIRECT_UNLINK:0x80,
	USE_REL_UNLINK:0x100,
	USE_CHECK:0x200
}
KGL.INHERIT=KGL.ID_1_IS_GROUP|KGL.USE_ID_2|KGL.ID_2_IS_GROUP|KGL.USE_DIRECT_LINK|KGL.USE_DIRECT_UNLINK|KGL.USE_REL_UNLINK|KGL.USE_CHECK;
KGL.EXPAND_BASE=KGL.USE_ID_1|KGL.OUTPUT_IS_CHILD;
KGL.USE_COLUM_2=KGL.USE_DIRECT_LINK|KGL.USE_DIRECT_UNLINK|KGL.USE_REL_UNLINK;

function toggleGroupTree(sub_root,id1,id2,type) {
	var next=sub_root.nextSibling;
	while(next != undefined && next.nodeType == 3) next=next.nextSibling;
	if(sub_root.className.indexOf("tree-open")>=0) {
		sub_root.className = sub_root.className.replace(" tree-open","");
		while(isSubNode(sub_root,next)) {
			next=hideTableTree(next);
		}
	} else {
		sub_root.className += " tree-open";
		if(sub_root.className.indexOf("loaded")<0) {
			sub_root.className += " loaded";
			ajaxOpen("ajax.php?action=group&type=" + type + "&id1=" + id1 + "&id2=" + id2,
				function(r){
					var parent=sub_root.parentNode;
					var indent=parseInt(sub_root.children[0].style.paddingLeft)+16;
					convertGroupList(parent,sub_root.rowIndex+1,r,id1,id2,indent,type);
				});
		}
		while(isSubNode(sub_root,next)) {
			next=showTableTree(next);
		}
	}
}

function convertGroupList(table,index,list,id1,id2,indent,type) {
//	return "<tr><td>" + list.replace("<hr>","</td></tr><tr><td>").replace("<br>","</td><td>") + "</td></tr>";
//type=0: [title|link to]
	var rows=list.split("<hr>");
	var i;
	var next_type=(type & KGL.INHERIT) | KGL.EXPAND_BASE;
	for(i=0;i<rows.length;i++) {
		var cols=rows[i].split("<br>");
		if(cols.length<4)continue;
		var tr=table.insertRow(index+i);
		tr.id="kt_group_" + cols[1];
		tr.className="kt_group_" + id1;
		var td=tr.insertCell(-1);
		td.style.paddingLeft=indent+"px";
		var tmp="";
		if(type & KGL.USE_CHECK) {
			var toggle_select="onchange='toggleBoxClass(this.parentNode.parentNode,\"row_selected\")'"
			if(cols[4]==undefined) {
				tmp+="<input type=checkbox name='links[" + cols[1] + "]' value='1' " + toggle_select + "/>";
			} else {
				tmp+="<input type=checkbox name='unlinks[" + cols[1] + "]' value='1' checked " + toggle_select + "/>";
				tmp+="<input type=hidden name='unlinks0[" + cols[1] + "]' value='1' />";
				tr.className+=" row_selected";
			}
			tr.onclick=function(e){
				if(e.target.tagName.toLowerCase()=="td") {
					var cc=this.cells[0].childNodes;
					for(var cci=0;cci<cc.length;cci++)if(cc[cci].tagName!=undefined)
						if(cc[cci].tagName.toLowerCase()=="input" && cc[cci].type.toLowerCase()=="checkbox")
							cc[cci].click();//checked=!cc[cci].checked;
				}
			}
		}
		if(type & KGL.ID_1_IS_GROUP) {
			tmp+="<a onclick='toggleGroupTree(this.parentNode.parentNode," + cols[1] + "," + id2 + "," + next_type + ")' ";
			if(cols[2]==0)tmp+="style='padding-left:16px;margin-right:4px;'></a>";
			else tmp+="class='toggle-pm'></a>";
			tmp+="<a href='group.php?action=show&id=" + cols[1] + "'>" + cols[0] + "</a>(" + cols[2] + "/" + cols[3] + ")";
		} else {
			tmp+="<a href='shownode.php?id=" + cols[1] + "'>";
			tmp+=cols[0] + "</a>(" + cols[2] + ")";
		}
		td.innerHTML=tmp;
		tmp="";
		if(type & KGL.USE_DIRECT_LINK) {
			tmp+="[<a onclick='$(\"group_link_parent\").value=" + cols[1] + "'>" + $d.group_link_parent + "</a>]&nbsp;";
			tmp+="[<a onclick='$(\"group_link_child\").value=" + cols[1] + "'>" + $d.group_link_child + "</a>]&nbsp;";
		}
		if(type & KGL.USE_DIRECT_UNLINK) {
			if(id1>0)
				tmp+="[<a href='group.php?action=unlink&id1=" + id1 + "&id2=" + cols[1] + "&type=group'>" + $d.group_unlink + "</a>]&nbsp;";
		}
		if(type & KGL.USE_REL_UNLINK) {
			if(indent==0) {
				var container,component,linktype="node";
				if(type & KGL.OUTPUT_IS_CHILD){container=id2;component=cols[1];}
				else{container=cols[1];component=id2;}
				if((type & KGL.ID_1_IS_GROUP)&&(type & KGL.ID_2_IS_GROUP))linktype="group";
				tmp+="[<a href='group.php?action=unlink&id1=" + container + "&id2=" + component + "&type=" + linktype + "'>" + $d.group_unlink + "</a>]&nbsp;";
			}
		}
		if(type & KGL.USE_COLUM_2)tr.insertCell(-1).innerHTML=tmp;
	}
}

function drawGroupList(cell_add,id1,id2,type) {
//type=0:  show-group	[title|link-from,link-to[,unlink]]
//type=2:  node-group   [title]
//type=3:  show-node	[title|unlink]
//type=31: link-node	[title|check]
	var cell_inner=cell_add.innerHTML;
	cell_add.innerHTML="loading...";
	ajaxOpen("ajax.php?action=group&id1=" + id1 + "&id2=" + id2 + "&type=" + type,
		function(r){
			var parent=cell_add.parentNode.parentNode;
			var row_index=cell_add.parentNode.rowIndex;
			convertGroupList(parent,row_index,r,id1,id2,0,type);
			cell_add.innerHTML=cell_inner;
		});
}

//check title
function checkTitle(input,output,type) {
	ajaxOpen("ajax.php?action=check_title&type=" + type + "&title=" + encodeURIComponent(input.value),
		function(r){
			if(r.length==0)output.innerHTML="";
			else {
				var rows=r.split("<br>");
				var table=document.createElement('table');
				var i;
				for(i=0;i<rows.length;i++) {
					var td=table.insertRow(i).insertCell(0);
					td.style.borderColor="black";
					td.style.borderStyle="solid";
					td.style.borderWidth="1px";
					td.innerHTML=rows[i];
				}
				table.style.minWidth=input.clientWidth + "px";
				output.innerHTML="";
				output.appendChild(table);
			}
		});
}

//cookie
function addSimpleCookie(n,v,days){
	var date=new Date();
	date.setTime(date.getTime+days*1000*3600*24);
	document.cookie=n + "=" + v + "; expire=" + date.toGMTString();
} 

